package com.tengju.bff.interfaces.manage.crm.motivation;

import com.tengju.bff.interfaces.covercharge.util.CsvExportUtil;
import com.tengju.bff.interfaces.manage.crm.motivation.facade.MotivationPerformanceObjectiveFacade;
import com.tengju.bff.interfaces.manage.crm.motivation.form.AddPerformanceObjectiveForm;
import com.tengju.bff.interfaces.manage.crm.motivation.form.GetPerformanceObjectiveCommonListForm;
import com.tengju.bff.interfaces.manage.crm.motivation.form.RemovePerformanceObjectiveForm;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.motivation.application.performance.MotivationPerformanceObjectiveService;
import com.tengju.motivation.application.performance.dto.ChunMiaoPlanUserTargetForm;
import com.tengju.motivation.application.shared.ExcelAnalysisUtil;
import com.tengju.user.domain.model.user.Staff;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: 李大仙
 * @Date: 2021/3/11 10:36 上午
 */
@Validated
@RestController
@Api(tags = "绩效目标管理")
@RequestMapping(value = "manage/crm/motivation/performance/objective")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MotivationPerformanceObjectiveController {

    private final MotivationPerformanceObjectiveService performanceObjectiveService;
    private final MotivationPerformanceObjectiveFacade performanceObjectiveFacade;

    @ApiOperation(value = "分页查询常规绩效目标列表", notes = "分页查询常规绩效目标列表")
    @RequestMapping(value = "getByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getCommonListByPage(@Valid GetPerformanceObjectiveCommonListForm form) {
        return ApiResponse.newSuccess(performanceObjectiveFacade.getCommonListByPage(form));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Integer> add(@RequestBody AddPerformanceObjectiveForm form) {
        Staff staff = ManageUtil.getOperateUser();
        return ApiResponse.newSuccess(performanceObjectiveService.add(
                form.getObjectId(),
                form.getType(),
                form.getKey(),
                form.getName(),
                form.getDesc(),
                form.getValue(),
                form.getStartTime(),
                form.getEndTime(),
                staff.getName()));
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Integer> remove(@Valid @RequestBody RemovePerformanceObjectiveForm form) {
        return ApiResponse.newSuccess(performanceObjectiveService.remove(form.getIdList()));
    }

    @ApiOperation(value = "获取绩效目标模版文件下载地址", notes = "获取绩效目标模版文件下载文件")
    @RequestMapping(value = "/getTemplate", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<String> getPerformanceObjectiveTemplate() {
        return ApiResponse.newSuccess(performanceObjectiveService.getPerformanceObjectiveTemplate());
    }

    @ApiOperation(value = "获取导入说明", notes = "获取导入说明")
    @RequestMapping(value = "/getImportDesc", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<List<String>> getImportDesc() {
        return ApiResponse.newSuccess(performanceObjectiveFacade.getImportDesc());
    }

    @ApiOperation(value = "导入常规绩效目标", notes = "导入常规绩效目标")
    @RequestMapping(value = "/import", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> importPerformanceObjective(@RequestParam("file") MultipartFile multipartFile,
                                                           @RequestParam("type") Integer type) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        String operatorName = servletContext.getRealName();
        List<String[]> rows = ExcelAnalysisUtil.analysis(multipartFile);
        performanceObjectiveService.importPerformanceObjective(rows, type, operatorName);
        return ApiResponse.newSuccess();
    }

    @ApiOperation(value = "导出常规绩效目标", notes = "导出常规绩效目标")
    @RequestMapping(value = "/export", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Object> exportPerformanceObjective(GetPerformanceObjectiveCommonListForm form) throws Exception {
        form.setPageIndex(1);
        form.setPageSize(50000);
        TableVO tableVO = performanceObjectiveFacade.exportPerformanceObjective(form);
        HttpServletRequest request = ServletContextUtil.getRequest();
        HttpServletResponse response = ServletContextUtil.getResponse();
        String fileName = "常规绩效目标KPI";
        CsvExportUtil.responseSetProperties(fileName, request, response);
        CsvExportUtil.doExport(tableVO, response.getOutputStream());
        return ApiResponse.newSuccess();
    }


    @ApiOperation(value = "修改春苗用户目标", notes = "修改春苗用户目标")
    @RequestMapping(value = "updateChunMiaoTarget", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Void> updateChunMiaoTarget(@RequestBody ChunMiaoPlanUserTargetForm updateUserTargetForm) {
        Staff staff = ManageUtil.getOperateUser();
        performanceObjectiveService.updateChunMiaoTarget(updateUserTargetForm, staff.getName());
        return ApiResponse.newSuccess();
    }

    @ApiOperation(value = "查找春苗用户目标", notes = "查找春苗用户目标")
    @RequestMapping(value = "chunMiaoTargetQuery", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<ChunMiaoPlanUserTargetForm> chunMiaoTargetQuery(@RequestParam @NotNull(message = "会员id不能为空") Long idCode, @RequestParam @NotNull(message = "年度信息不能为空") Integer dateForYear) {
        return ApiResponse.newSuccess(performanceObjectiveFacade.queryChunMiaoTarget(idCode, dateForYear));
    }
}
